import hashlib
import random
import re
import requests
import time
import base64
from fontTools.ttLib import TTFont

url = 'https://piaofang.maoyan.com/dashboard-ajax/movie'

headers = {
	"Accept": "application/json, text/plain, */*",
	"Accept-Language": "zh-CN,zh;q=0.9",
	"Cache-Control": "no-cache",
	"Connection": "keep-alive",
	"Pragma": "no-cache",
	"Referer": "https://piaofang.maoyan.com/dashboard/movie?movieId=1448719",
	"Sec-Fetch-Dest": "empty",
	"Sec-Fetch-Mode": "cors",
	"Sec-Fetch-Site": "same-origin",
	"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
	"X-FOR-WITH": "DuknV9Rst0hx6yE+oUD606+wpRECdA0NKGpuyvIjKzdvWCCMeFwD58nLOOkiF4uWCPfJTb8dQsG0dQTUOhs06lBjLzWVmYnlaXLkc5AyvRl1MIWJ+FdtPd3QFIT/8nwPtnp73QBWL9mPNKras3SekUz9VwgiLUHJT2YE1oDYAC8L1dhI0FPKdheaYUU+Zl1CvQhDjAcGRNL81DvEm6rl0g==",
	"sec-ch-ua-mobile": "?0"
}

cookies = {
	"_lxsdk_cuid": "193230d50d63d-046217c3f26cff-26011951-144000-193230d50d7c8",
	"_lxsdk": "193230d50d63d-046217c3f26cff-26011951-144000-193230d50d7c8",
	"_lx_utm": "utm_source%3DBaidu%26utm_medium%3Dorganic",
	"_lxsdk_s": "193293ccabf-4c1-fea-451%7C%7C1"
}


time_stamp = int(time.time() * 1000)
ua_base64 = base64.b64encode(
	'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36'.encode()).decode()
index = random.randint(1, 1000)
c = f'method=GET&timeStamp={time_stamp}&User-Agent={ua_base64}&index={index}&channelId=40009&sVersion=2&key=A013F70DB97834C0A5492378BD76C53A'
c = re.sub(r'\s+', ' ', c)
sing_key = hashlib.md5(c.encode()).hexdigest()

params = {
	"movieId": "1448719",
	"orderType": "0",
	"uuid": "193230d50d63d-046217c3f26cff-26011951-144000-193230d50d7c8",

	"timeStamp": f"{time_stamp}",
	"User-Agent": f"{ua_base64}",

	"index": f"{index}",
	"channelId": "40009",
	"sVersion": "2",

	"signKey": f"{sing_key}"
}

# 发送GET请求
response = requests.get(url, headers=headers, cookies=cookies, params=params)

chars = response.json()['movieList']['nationBoxInfo']['nationSplitBoxSplitUnit']['num']

font_url = response.json()['fontStyle'][-83:-4]

print(chars, f"http:{font_url}")

font_res = requests.get(f"http:{font_url}")
with open("font.woff", "wb") as f:
	f.write(font_res.content)

font = TTFont("font.woff")
font.saveXML("font.xml")


with open("font.xml", "r") as f:
	content = f.read()
	item = {}
	glyphs = re.findall(r'<GlyphID id="(.*?)" name="(.*?)"/>', content)[2:]
	for glyph in glyphs:
		item[glyph[1]] = int(glyph[0]) - 2
	# print(len(item))

	result = {}
	maps = re.findall(r'<map code="0(.*?)" name="(.*?)"/>', content)
	# print(maps)
	for map in maps:
		if map[1] in item:
			result[f"&#{map[0]}"] = item[map[1]]

	print(result)

	num_str = ''
	datas = [chars[0:8]] + [chars[8:16]] + [chars[16:24]] + ["."] + [chars[25:]]
	for data in datas:
		if data == ".":
			num_str += "."
		else:
			# print(result[data[:-1]])
			num_str += f"{result[data[:-1]]}"
	print(num_str)